package com.amigo;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.amigo.helper.Constants;
import com.amigo.helper.JsonFileUtility;
import com.amigo.helper.ServiceHandler;

public class Settings extends Activity {
	CheckBox male, female, both;
	String gender_pref = "", location, interests;
	EditText Location, Interests;
	TextView Edit_location, Edit_interests;
	
	ProgressDialog ringProgressDialog = null;
	
	private String url_pref_gender = Constants.base_url + "updatePrefGender" ; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_settings);

		male = (CheckBox) findViewById(R.id.checkBox_male);
		female = (CheckBox) findViewById(R.id.checkBox_female);
		both = (CheckBox) findViewById(R.id.checkBox_both);
		Interests = (EditText) findViewById(R.id.settings_interests);
		Location = (EditText) findViewById(R.id.settings_location);
		Edit_interests = (TextView) findViewById(R.id.edit_interests);
		Edit_location = (TextView) findViewById(R.id.edit_location);

		try {
			JSONObject user_object = new JSONObject(
					JsonFileUtility.ReadJsonData(Constants.TAG_UserObject));
			gender_pref = user_object.getString("pref_gender");
			location = user_object.getString("location");
			interests = user_object.getString("interests");
			if (gender_pref.equals("both")) {
				both.setChecked(true);
			} else if (gender_pref.equals("male")) {
				male.setChecked(true);
			} else if (gender_pref.equals("female")) {
				female.setChecked(true);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		Location.setText(location);
		Location.setEnabled(false);

		Interests.setText(interests);
		Interests.setEnabled(false);

		Edit_interests.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Interests.setText("");
				Interests.setEnabled(true);
				Edit_interests.setVisibility(View.GONE);

			}
		});

		Edit_location.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Location.setText("");
				Location.setEnabled(true);
				Edit_location.setVisibility(View.GONE);

			}
		});

		both.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (male.isChecked()) {
					male.setChecked(false);
				}
				if (female.isChecked()) {
					female.setChecked(false);
				}

				gender_pref = "both";

			}
		});

		male.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if (both.isChecked()) {
					both.setChecked(false);
				}
				gender_pref = "male";
			}
		});

		female.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if (both.isChecked()) {
					both.setChecked(false);
				}
				gender_pref = "female";
			}
		});
	}
	
	public void save(View v )
	{
		if (male.isChecked() && female.isChecked())
		{
			gender_pref = "both";
		}
		if (male.isChecked())
		{
			gender_pref = "male";
		}
		if (female.isChecked())
		{
			gender_pref = "female";
		}
		location = Location.getText().toString();
		interests = Interests.getText().toString();
		
		new UpdateGenderPrefrenceTask().execute();
	}

	
	private class UpdateGenderPrefrenceTask extends AsyncTask<Void, Void, Void>
	{
		@Override
		protected void onPreExecute() {
			ringProgressDialog = ProgressDialog.show(Settings.this,
					"Saving","Saving your Settings",
					true);
			ringProgressDialog.setCancelable(false);
			super.onPreExecute();
		}
		@Override
		protected Void doInBackground(Void... param) {
			ServiceHandler sh = new ServiceHandler();
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("id", JsonFileUtility.ReadJsonData(Constants.TAG_UserGUID)));
			params.add(new BasicNameValuePair("prefGender", gender_pref));
			String jsonStr = sh.makeServiceCall(url_pref_gender, ServiceHandler.POST,
					params);

			Log.d("Response: ", "> " + jsonStr);
			
			
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
		if (ringProgressDialog.isShowing())
		{
			ringProgressDialog.dismiss();
		}
			super.onPostExecute(result);
		}
		
		//TODO handle empty errors and start update profile task 
		
	}
	public void prev(View v) {
		startActivity(new Intent(this, Dashboard.class));
		overridePendingTransition(android.R.anim.slide_in_left,
				android.R.anim.slide_out_right);
	}
}
